﻿using SSAT.Forms;
using SSAT.Utils;

namespace SSAT.Commands
{
    internal sealed class ReconnectCommand : CommandBase
    {
        private readonly MainForm _mainForm;

        public override string DisplayName
        {
            get { return CommandsResources.ReconnectCommandDisplayName; }
        }

        public ReconnectCommand(MainForm form)
        {
            Verification.CheckArgumentForNull(form, "form");
            _mainForm = form;
        }

        public override void Execute()
        {
            if (_mainForm == null || _mainForm.CurrentConnection == null)
            {
                return;
            }

            AppConsole.WriteLine(string.Format(Resources.Reconnecting, ConnectionHelper.GetServerName(_mainForm.CurrentConnection)));

            _mainForm.SetCurrentConnection(_mainForm.CurrentConnection);
            ConnectionHelper.OpenConnection(_mainForm.CurrentConnection, false, false);
        }
    }
}
